<?php
namespace Attendance\Controller;
use Common\Controller\AdminbaseController;
use Home\Service\ClockTimeService;
// +----------------------------------------------------------------------
// | Date:2017年3月1日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class FestivalController extends AdminbaseController{
    protected $cache_key = 'festival_day';
    public function _initialize(){
        parent::_initialize();
        $this->model = D('Festival');
    }
    
    public function index(){
        $list = $this->lists($this->model,$where,$order='date desc');
        $this->assign('list',$list);
        $this->display();
    }
    
    
    public function add(){
        if (IS_POST) {
            $post = I('post');
            $post['created_time'] = CURRENT_TIME;
            if (empty($post['title'])) $this->error('请输入节日名称');
            //计算时间间隔，生成记录
            $start = $post['start_date'];
            $end = $post['end_date'];
            $start_timpstamp = strtotime($start);
            $end_timpstamp = strtotime($end);
            $interval = $end_timpstamp-$start_timpstamp;
            $intDays = $interval/86400;
            $festival = [];
            for ($i=0;$i<=$intDays;$i++){
                $curdatetime = strtotime("+{$i} day",$start_timpstamp);
                $curdate = date('Y-m-d',$curdatetime);
                $clockService = new ClockTimeService("$curdate {$post['start_time']}","$curdate {$post['end_time']}");
                $worktime = $clockService->get_intrval_work_time('min');

                $base_add = [
                    'date'=>$curdate,
                    'created_time'=>CURRENT_TIME,
                    'title'=>$post['title'],
                    'work_min'=>$post['type'] == 1 ? 0-$worktime : $worktime,
                    'week'=>date('w',$curdatetime),
                ];
                $add[] = $base_add+$post;
            }            
            
            
            $ret = $this->model->addAll($add);
            if (!$ret) $this->error('添加失败！');
            $this->writeCache();
            $this->success('添加成功！');
        }
        
        $this->assign('status',$this->status);
        $this->display();
    }
    
    public function writeCache(){
       $ret = $this->model->getField('date,type,ABS(work_min) as work_min');
       F($this->cache_key,$ret);
    }
    
    /**
     * 删除数据,可批量
     * @param unknown $model
     * date:2016年11月22日
     * author: EK_熊
     */
    public function delete($model=''){
        $model = empty($model) ? $this->model : $model;
        $ids = I('ids');
        $pk = $model->getPk();
        $map[$pk] = array('in',$ids);
        $ret = $model->where($map)->delete();
        if (!$ret) $this->error('操作失败,网络异常！');
        $this->writeCache();
        $this->success('操作成功！');
    }
    
    public function edit(){
    
        $id = I('id');
        $pk = $this->model->getPk();
        if (IS_POST){
            $post = I('post');
            $clockService = new ClockTimeService("{$post['start_date']} {$post['start_time']}","{$post['start_date']} {$post['end_time']}");
            $worktime = $clockService->get_intrval_work_time('min');
            $post['work_min'] = $post['type'] == 1 ? 0-$worktime : $worktime;
            $post['week'] = date('w',strtotime($post['start_date']));
            $where[$pk] = $id;
            $ret = $this->model->where($where)->save($post);
            if (!$ret) $this->error('操作失败！');
            
            $this->writeCache();
            $this->success('操作成功！');
        }
        $info = $this->model->where([$pk=>$id])->find();
        $this->assign('status',$this->status);
        $this->assign('info',$info);
        $this->display();
    }
    
    
}